#include <assert.h>

template<class node>
class handleMap
{
public:
	handleMap(int size):_nowindex(0),_space(size)
	{
		_node = new node*[_space];
		memset(_node, 0, sizeof(node*)*_space);
	}
	~handleMap()
	{
		delete[] _node;
	}
	node* getSomthing(const unsigned int handle)
	{
		unsigned int index = (handle&0xffff0000) >>16;
		if (index > _space)
		{
			return 0;
		}
		if (_node[index] && (*((int*) &_node[index])&0xffff) == (handle&0xffff))
		{
			return _node[index];
		}
		else
		{
			//assert(1==2 && "handle is destroyed");
			return 0;
		}
	}
	void release(const unsigned int handle)
	{
		unsigned int index = (handle&0xffff0000) >>16;
		if (index > _space)
		{
			return;
		}
		if (_node[index] && (*((int*)&_node[index])&0xffff) == (handle&0xffff))
		{
			_node[index] = 0;
		}
	}
	int getMap(node* something)
	{
		for(unsigned int i = _nowindex; i < _space; ++i)
		{
			if (_node[i]== 0)
			{
				_node[i] = something;
				_nowindex = i+1;
				int iret = (i<<16) | (0xffff& *((int*)&something));
				return iret;
			}
		}
		for(unsigned int i = 0; i < _nowindex; ++i)
		{
			if (_node[i]== 0)
			{
				_node[i] = something;
				_nowindex = i+1;
				int iret = (i<<16) | (0xffff& *((int*)&something));
				return iret;
			}
		}
		return 0;
	}
	int getHandle(node* something)
	{
		for(unsigned int i = 0; i < _space; ++i)
		{
			if (_node[i] == something)
			{
				return (i<<16) | (0xffff& *((int*)&something));
			}
		}
		assert(1==2 && "handle is empty");
		return 0;
	}
private:
	unsigned int _space;
	unsigned int _nowindex;
	node** _node; 
};